package com.ecoolex.ying.space.api.graphql;

import com.alibaba.fastjson.JSON;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberAuthority;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.ying.space.entity.*;
import com.ecoolex.ying.space.pojo.PickGoodsParametersDTO;
import com.ecoolex.ying.space.pojo.QueryPickGoodsDTO;
import com.ecoolex.ying.space.pojo.SearchPickGoodsDTO;
import com.ecoolex.ying.space.ws.DesignerProjectService;
import com.ecoolex.ying.space.ws.GoodsService;
import com.ecoolex.ying.space.ws.PickGoodsService;
import com.google.common.collect.Lists;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import java.util.List;
import java.util.stream.Collectors;

@GraphQL2Module(type = GraphQL2Module.Type.User, description = "商品分类查询")
public class PickGoodsGraphql {
    @Reference
    private GoodsService goodsService;
    @Reference
    private PickGoodsService pickGoodsService;
    @Reference
    private DesignerProjectService designerProjectService;
    @Autowired
    private StorageService storageService;

    @TypeField(type = PickGoodsWithBLOBs.class, field = "goods", description = "商品")
    public GoodsWithBLOBs goods(@Source PickGoodsWithBLOBs source) {
        return goodsService.getGoodsById(source.getGoodsId());
    }

    @TypeField(type = PickGoodsWithBLOBs.class, field = "classify", description = "分类")
    public GoodsClassify classify(@Source PickGoodsWithBLOBs source) {
        return Check.isNull(source.getClassifyId()) ? null : goodsService.getClassifyById(source.getClassifyId());
    }

    @TypeField(type = PickGoodsWithBLOBs.class, field = "labelList", description = "标签列表")
    public List<GoodsLabel> labelList(@Source PickGoodsWithBLOBs source) {
        return goodsService.queryLabelByPickGoodsId(source.getPickGoodsId());
    }

    @TypeField(type = PickGoodsWithBLOBs.class, field = "isCollected", description = "是否收藏")
    public boolean isCollected(@Source PickGoodsWithBLOBs source, ConsumerMemberAuthority user) {
        if (user.isLogin()) {
            return designerProjectService.existPickGoodsFavoriteByUserIdAndPickGoodsId(user.userId(), source.getPickGoodsId());
        }
        return false;
    }

    @TypeField(type = PickGoodsWithBLOBs.class, field = "image", description = "图片")
    public StorageInfo image(@Source PickGoodsWithBLOBs source) {
        return storageService.get(source.getImage());
    }

    @TypeField(type = PickGoodsWithBLOBs.class, field = "shopwindowImages", description = "橱窗图片列表")
    public List<StorageInfo> shopwindowImages(@Source PickGoodsWithBLOBs source) {
        if (Check.notNullOrTrimEmpty(source.getShopwindowImages())) {
            return JSON.parseArray(source.getShopwindowImages(), String.class).stream()
                    .map(storageService::get)
                    .filter(Check::notNull)
                    .collect(Collectors.toList());
        }
        return Lists.newArrayList();
    }

    @TypeField(type = PickGoodsWithBLOBs.class, field = "parameters", description = "参数")
    public PickGoodsParametersDTO parameters(@Source PickGoodsWithBLOBs source) {
        return Check.isNullOrTrimEmpty(source.getParameters()) ? null : JSON.parseObject(source.getParameters(), PickGoodsParametersDTO.class);
    }

    @TypeField(type = PickGoodsWithBLOBs.class, field = "productList", description = "产品列表")
    public List<PickGoodsProduct> productList(@Source PickGoodsWithBLOBs source) {
        return pickGoodsService.queryProductByPickGoodsId(source.getPickGoodsId());
    }

    @TypeField(type = PickGoodsProduct.class, field = "product", description = "产品")
    public GoodsProduct product(@Source PickGoodsProduct source) {
        return goodsService.getProductById(source.getProductId());
    }

    @TypeField(type = PickGoodsProduct.class, field = "image", description = "产品图片")
    public StorageInfo image(@Source PickGoodsProduct source) {
        return storageService.get(source.getImage());
    }

    @TypeField(type = PickGoodsWithBLOBs.class, field = "channelList", description = "渠道列表")
    public List<PickGoodsChannel> channelList(@Source PickGoodsWithBLOBs source) {
        return pickGoodsService.queryChannelByPickGoodsId(source.getPickGoodsId());
    }

    @TypeField(type = PickGoodsProduct.class, field = "pickGoods", description = "优选商品")
    public PickGoodsWithBLOBs coverImage(@Source PickGoodsProduct source) {
        return pickGoodsService.getPickGoodsById(source.getPickGoodsId());
    }

    @QueryField(field = "pickGoods", description = "查询优选商品详情")
    public PickGoodsWithBLOBs query(@NonNull Integer pickGoodsId, ConsumerMemberAuthority user) {
        user.assertLogin();
        return pickGoodsService.getPickGoodsById(pickGoodsId);
    }

    @QueryField(field = "pickGoodsList", description = "查询优选商品列表")
    public Pagination<PickGoodsWithBLOBs> query(@NonNull QueryPickGoodsDTO query, @NonNull Page page, ConsumerMemberAuthority user) {
        user.assertLogin();
        return pickGoodsService.queryPickGoods(query, null, page);
    }

    @QueryField(field = "searchPickGoodsList", description = "搜索优选商品列表")
    public Pagination<PickGoodsWithBLOBs> searchPickGoodsList(@NonNull SearchPickGoodsDTO query, @NonNull Page page, ConsumerMemberAuthority user) {
        user.assertLogin();
        return pickGoodsService.searchPickGoods(query, page);
    }

    @QueryField(field = "pickGoodsListByUserFavorite", description = "根据用户收藏夹查询优选商品列表")
    public Pagination<PickGoodsWithBLOBs> queryPickGoodsByUserFavorite(String keyword, @NonNull Page page, ConsumerMemberAuthority user) {
        return pickGoodsService.queryPickGoodsByUserFavorite(user.userId(), keyword, page);
    }
}
